<?php

namespace App\Http\Controllers;
require __DIR__ . '/../Sockets/client.php';

use App\Models\pc_company\StaffStatus;
use App\Models\pc_company\Station;
use App\Models\pc_platform\TaskVehicle;
use App\Models\pc_platform\User;
use App\Models\pc_platform\UserAuthDriver;
use App\Models\pc_station\StationDetail;
use App\Models\pc_station\Task;
use App\Models\pc_station\TasksVehicle;
use App\Models\pc_station\Vehicle;
use App\Models\PushModels;
use App\Models\TasksPlane;
use GuzzleHttp\Client;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Zhuzhichao\BankCardInfo\BankCard;

class SocketController extends Controller
{
    /**
     * socket 链接消息
     * URL: POST /api/pc_platform/station/task/list/detail
     * @param id int 审核id
     * @return JSON { code: 状态码, msg: 信息描述 , result: 返回结果 }
     *  类型: notice
     * 作用: 发送通知
     * 触发方式: 当系统需要发送通知时
     * 数据: `{
     * targetType : '接收消息对象类型', # 接收消息对象类型 0: 通知后台, 1: 通知搅拌站, 2: 通知 用户, 3:对用户发起广播, 4: 对所有的在线的发起广播
     * target : '接收消息对象ID', #接收消息对象ID 可为数值,可为数组, 可为''
     * type : '消息类型', #当前消息的类型
     * title : '消息标题', # 当前司机的ID
     * message : '消息内容', # 消息的文字内容,
     * extend : {}, #消息扩展参数
     * }`219.153.15.99
     */
    //219.153.15.99
    public function web_socket()
    {



////        DB::table('somorn_message_log')->insert(['targetType'=>1,'type'=>2,'msg'=>$mes,'target'=>$task->somorn_station_id,'title'=>$title,'time'=>time()]);
//        $web_socket = new \WebSocketClient();
////        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
//        $web_socket->connect('219.153.15.99', '3004', '/');
//        $re = $web_socket->sendData('notice', $message);
        dd(1);
//      //获取任务
        $task = DB::table('somorn_tasks')->where('somorn_tasks_id', 271)->first();
        //车主取消订单
        //获取用户
        $user = DB::table('somorn_users')->where('id', 407)->first();
        //发送给搅拌站
        $title = '车主取消订单';
        $mes = '【拉法多】 您好' . $task->tasks_no . '的任务已经被车主' . $user->username . '取消了';

        //发送给搅拌站

        //发给司机
        //获取司机
        $staff = DB::table('somorn_staff_status')->where('boss_id', 407)->get()->toArray();
        $driver = array_column($staff, 'user_id');
        $title = '车主取消订单';
        $mes = '【拉法多】 您好' . $task->tasks_no . '的任务已经被车主' . $user->username . '取消了';
        $message = [
            'targetType' => 2,
            'target' => $driver,
            'type' => 2,
            'title' => $title,
            'message' => $mes
        ];

        foreach ($driver as $v) {
            DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $v, 'title' => $title, 'time' => time()]);
        }

        $web_socket = new \WebSocketClient();
//            $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        $re = $web_socket->sendData('notice', $message);

    }

    /**
     * socket 搅拌站发布新的注册申请
     * URL: POST /api/pc_platform/station/task/list/detail
     * @param id int 审核id
     * @param status int 状态
     * @param user_id int 用户id
     * @return JSON { code: 状态码, msg: 信息描述 , result: 返回结果 }
     *  类型: notice
     *  '接收消息对象类型', # 接收消息对象类型 0: 通知后台, 1: 通知搅拌站, 2: 通知 用户, 3:对用户发起广播, 4: 对所有的在线的发起广播
     * 1 新用户（车主、驾驶员、搅拌站）提交注册申请
     */
    public function information($status, $id)
    {

        //注册类型状态
        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        switch ($status) {
            //搅拌站注册
            case 1:
                $station = StationDetail::where('somorn_station_id', $id)->first(['title']);
                $titel = '搅拌站/集团注册';
                $msg = "【拉法多】您好，{$station->title}发起了新的注册申请，请及时查看审核！";
                DB::table('somorn_message_log')->insert(['targetType' => 0, 'type' => 1, 'msg' => $msg, 'target' => 1, 'title' => $titel, 'time' => time()]);
                $message = [
                    'targetType' => 0,
                    'target' => 1,
                    'title' => $titel,
                    'type' => 1,//1系统通知2订单通知3行业咨询
                    'message' => $msg,
                    'extend' => ['type' => 6]

                ];
                break;
            //搅拌站的车主注册
            case 2:
                $titel = '注册申请';
                $msg = "【拉法多】您好，搅拌站/发起了新的注册申请，请及时查看审核！";
                $status = 1;
                $message = [
                    'targetType' => 0,
                    'target' => 1,
                    'title' => $titel,
                    'type' => 1,//1系统通知2订单通知3行业咨询
                    'message' => $msg,
                    'extend' => ['type' => 6]
                ];
                break;
            //驾驶员
            case 7:
                $station = StationDetail::where('somorn_station_id', $id)->first(['title']);
                $msg = "【拉法多】您好，{$station->title}发起了新的注册申请，请及时查看审核！";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => 1, 'title' => '搅拌站/集团注册', 'time' => time()]);
                $message = [
                    'targetType' => 0,
                    'target' => $id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '搅拌站/集团注册',
                    'message' => $msg,
                    'extend' => ['type' => 6]
                ];
                break;
            case 8:
                $info = DB::table('somorn_auth_enterprise')->where('user_id', $id)->orderBy('id', 'desc')->first();
                $msg = "【拉法多】您好，{$info->name}企业车主审核已通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $id, 'title' => '企业车主审核', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '企业车主审核',
                    'message' => $msg,
                    'extend' => ['type' => 1]
                ];
                break;
            case 9:
                $info = DB::table('somorn_auth_enterprise')->where('user_id', $id)->orderBy('id', 'desc')->first();
                $msg = "【拉法多】您好，{$info->name}企业车主审核未通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $id, 'title' => '企业车主审核', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '企业车主审核',
                    'message' => $msg,
                    'extend' => ['type' => 1]
                ];
                break;
            case 10:
                $info = DB::table('somorn_auth_real_name')->where('user_id', $id)->orderBy('id', 'desc')->first();
                $msg = "【拉法多】您好，{$info->name}车主实名审核已通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $id, 'title' => '车主实名审核', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '车主实名审核',
                    'message' => $msg,
                    'extend' => ['type' => 1]
                ];
                break;
            case 11:
                $info = DB::table('somorn_auth_real_name')->where('user_id', $id)->orderBy('id', 'desc')->first();
                $msg = "【拉法多】您好，{$info->name}车主实名审核未通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $id, 'title' => '车主实名审核', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '车主实名审核',
                    'message' => $msg,
                    'extend' => ['type' => 1]
                ];
                break;
            case 12:
                $info = DB::table('somorn_staff_status')->where('user_id', $id)->first();
                $msg = "【拉法多】您好，司机认证审核已通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $info->boss_id, 'title' => '司机认证', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $info->boss_id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '司机认证',
                    'message' => $msg,
                    'extend' => ['type' => 4]
                ];
                break;
            case 13:

                $info = DB::table('somorn_staff_status')->where('user_id', $id)->first();
                $msg = "【拉法多】您好，司机认证审核未通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $info->boss_id, 'title' => '司机认证', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $info->boss_id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '司机认证',
                    'message' => $msg,
                    'extend' => ['type' => 4]
                ];
                break;
            case 14:
                $info = DB::table('somorn_vehicle')->where('somorn_vehicle_id', $id)->first();

                $msg = "【拉法多】您好，车辆认证审核已通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $info->user_id, 'title' => '车辆认证', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $info->user_id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '车辆认证',
                    'message' => $msg,
                    'extend' => ['type' => 6]
                ];
                break;
            case 15:
                $info = DB::table('somorn_vehicle')->where('somorn_vehicle_id', $id)->first();
                $msg = "【拉法多】您好，车辆认证审核未通过验证";
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $msg, 'target' => $info->user_id, 'title' => '车辆认证', 'time' => time()]);
                $message = [
                    'targetType' => 2,
                    'target' => $info->user_id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '车辆认证',
                    'message' => $msg,
                    'extend' => ['type' => 6]
                ];
                break;
            case 16:
                $info = DB::table('somorn_station_detail')->where('somorn_station_id', $id)->first();
                $msg = "【拉法多】{$info->title}申请挂靠集团";
                DB::table('somorn_message_log')->insert(['targetType' => 0, 'type' => 1, 'msg' => $msg, 'target' => $info->user_id, 'title' => '集团挂靠', 'time' => time()]);
                $message = [
                    'targetType' => 0,
                    'target' => 1,
                    'type' => 1,//1 注册 2 修改
                    'title' => '集团挂靠',
                    'message' => $msg,
                    'extend' => ['type' => 6]

                ];
                break;
            case 17:
                $info = DB::table('somorn_station_detail')->where('somorn_station_id', $id)->first();
                $msg = "【拉法多】{$info->title}申请挂靠集团审核失败";
                DB::table('somorn_message_log')->insert(['targetType' => 1, 'type' => 1, 'msg' => $msg, 'target' => $info->somorn_station_id, 'title' => '集团挂靠', 'time' => time()]);
                $message = [
                    'targetType' => 1,
                    'target' => $id,
                    'type' => 1,//1 注册 2 修改
                    'title' => '集团挂靠',
                    'message' => $msg,
                    'extend' => ['type' => 6]
                ];
                break;
        }
        $web_socket->sendData('notice', $message);
    }

    /**
     * socket  订单消息
     * URL: POST /api/pc_platform/station/task/list/detail
     * @param $id int 任务id
     * param $status 类型
     * @return JSON { code: 状态码, msg: 信息描述 , result: 返回结果 }
     * 用户（搅拌站、车主、司机）发起资料修改后的审核申请，应对后台给予系统通知
     *  '接收消息对象类型', # 接收消息对象类型 0: 通知后台, 1: 通知搅拌站, 2: 通知 用户, 3:对用户发起广播, 4: 对所有的在线的发起广播
     * 1 新用户（车主、驾驶员、搅拌站）提交注册申请
     * 1 系统通知 2 订单通知
     */
    public function order($status, $id)
    {

        switch ($status) {
            //车辆故障
            case 1:
                //汽车故障取消订单 通知车主和搅拌站
                $tasks_veh = DB::table('somorn_tasks_vehicle')->where(['somorn_tasks_vehicle_id' => $id, 'is_fault' => 1])->first();
                $task = DB::table('somorn_tasks')->where('somorn_tasks_id', $tasks_veh->somorn_tasks_id)->first();
                $vehicle = DB::table('somorn_vehicle')->where('somorn_vehicle_id', $tasks_veh->somorn_vehicle_id)->first();

                $mes = "【拉法多】您的订单：{$task->tasks_no}，车牌号为{$vehicle->plate_number}的车辆因故障原因取消当前运单";
                $title = '车辆因故障';
                $message = [
                    'targetType' => 2,
                    'target' => $vehicle->user_id,
                    'type' => 2,
                    'title' => $title,
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $mes, 'target' => $vehicle->user_id, 'title' => $title, 'time' => time()]);
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                unset($web_socket);
                //搅拌站
                $web_socket = new \WebSocketClient();
//                $web_socket->connect('socket.hanguosoft.com', '3004', '/');
                $web_socket->connect('219.153.15.99', '3004', '/');
                $message = [
                    'targetType' => 1,
                    'target' => $task->somorn_station_id,
                    'type' => 4,//车辆故障
                    'title' => $title,
                    'message' => $mes
                ];
                DB::table('somorn_message_log')->insert(['targetType' => 1, 'type' => 1, 'msg' => $mes, 'target' => $task->somorn_station_id, 'title' => $title, 'time' => time()]);
                $web_socket->sendData('notice', $message);
                break;
            //开始执行
            case  2:
                //根据车辆id查询汽车任务
                $task_veh = DB::table('somorn_tasks_vehicle')->where(['somorn_vehicle_id' => $id, 'status' => 1])->first();
                //查询任务
                $task = DB::table('somorn_tasks')->where('somorn_tasks_id', $task_veh->somorn_tasks_id)->first();
                //查询车辆表
                $veh = DB::table('somorn_vehicle')->where('somorn_vehicle_id', $id)->first();
                $mes = "【拉法多】您的订单：{$task->tasks_no}，车牌号为{$veh->plate_number}的车辆已开始执行订单。";
                $title = '车辆开始执行订单';
                //车主
                $message = [
                    'targetType' => 2,
                    'target' => $task->user_id,
                    'type' => 1,
                    'title' => $title,
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $mes, 'target' => $task->user_id, 'title' => $title, 'time' => time()]);
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                unset($web_socket);
                //搅拌站

                $message = [
                    'targetType' => 1,
                    'target' => $task->somorn_station_id,
                    'type' => 3, //车辆开始执行订单
                    'title' => '车辆开始执行订单',
                    'message' => $mes
                ];
                DB::table('somorn_message_log')->insert(['targetType' => 1, 'type' => 1, 'msg' => $mes, 'target' => $task->somorn_station_id, 'title' => $title, 'time' => time()]);
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                break;
            //完成订单
            case 3:
                //通过id务查询任务

                $tasks = DB::table('somorn_tasks')->find($id, ['tasks_no', 'somorn_station_id']);
                //查询那些汽车接了该任务
                $task_vehicle = DB::table('somorn_tasks_vehicle')->where('somorn_tasks_id', $id)->first();
                //获取车辆id
                foreach ($task_vehicle as $vv) {
                    $car[] = $vv->somorn_vehicle_id;
                }
                $vehile = DB::table('somorn_vehicle')->whereIn('somorn_vehicle_id', $car)->get();
                //完成订单 通知车主和搅拌站
                //通知车主
                //添加消息通知数据数据
                $title = '订单完成';
                $user_id = [];
                $plate_number = [];
                foreach ($vehile as $v) {
                    $mes = "【拉法多】您的订单：{$tasks->tasks_no}，车牌号为{$v->plate_number}的车辆已全部完成该订单。";
                    DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 1, 'msg' => $mes, 'target' => $v->user_id, 'title' => $title, 'time' => time()]);
                    $plate_number[$v->user_id][] = $v->plate_number;
                    $user_id[] = $v->user_id;
                }


                $mes = "【拉法多】您的订单：{$tasks->tasks_no}，车牌号为{$v->plate_number}的车辆已全部完成该订单。";
                $message = [
                    'targetType' => 2,
                    'target' => $user_id,
                    'type' => 1,
                    'title' => $title,
                    'message' => $mes,
                    'extend' => $plate_number,
                    'extend' => ['type' => 6]
                ];
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                unset($web_socket);

                //通知搅拌站搅拌站
                foreach ($vehile as $v) {
                    $mes = "【拉法多】您的订单：{$tasks->tasks_no}，车牌号为{$v->plate_number}的车辆已全部完成该订单。";
                    DB::table('somorn_message_log')->insert(['targetType' => 1, 'type' => 2, 'msg' => $mes, 'target' => $v->user_id, 'title' => $title, 'time' => time()]);
                    $plate_number[] = $v->plate_number;
                }
                $message = [
                    'targetType' => 1,
                    'target' => $tasks->somorn_station_id,
                    'type' => 2,//车辆完成订单
                    'title' => $title,
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                unset($web_socket);
                //通知平台
                $station = StationDetail::where('somorn_station_id', $tasks->somorn_station_id)->first();
                $mes = "【拉法多】您好，{$station->title}搅拌站订单（订单号：{$tasks->tasks_no}）已全部完成，请确认查看。";
                $message = [
                    'targetType' => 4,
                    'target' => 1,
                    'type' => 1,
                    'title' => $title,
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                unset($web_socket);
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);

                break;

            case 6:
                //搅拌站取消订单通知车主和司机

                //任务
                $task = Task::find($id);

                //获取车辆
                $vehicleIds = TaskVehicle::where(['somorn_tasks_id' => $id, 'is_cancel' => 2])->where('get_user_id', '<>', 0)->pluck('somorn_vehicle_id')->toArray();

                //任务车辆
                $vehicle = Vehicle::whereIn('somorn_vehicle_id', $vehicleIds);

                //获取车主
                $boosID = $vehicle->pluck('user_id')->toArray();

                $boosID = array_unique($boosID);


                //获取司机
                $userID = StaffStatus::whereIn('somorn_vehicle_id', $vehicleIds)->pluck('user_id')->toArray();

                $mes = "【拉法多】您参与的订单（订单号：{$task->tasks_no}），因搅拌站原因已取消该订单。";

                $message = [
                    'targetType' => 2,
                    'target' => $boosID,
                    'type' => 2,
                    'title' => '任务取消',
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                foreach ($boosID as $boos) {
                    DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $boos, 'title' => '任务取消', 'time' => time()]);
                }
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                unset($web_socket);


                $message = [
                    'targetType' => 2,
                    'target' => $userID,
                    'type' => 2,
                    'title' => '任务取消',
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                foreach ($userID as $value) {
                    DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $value, 'title' => '任务取消', 'time' => time()]);
                }
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);


                break;
            case 7:
                //根据车辆id查询汽车任务
                $task_veh = DB::table('somorn_tasks_vehicle')->where(['somorn_vehicle_id' => $id, 'status' => 1])->first();
                //查询任务
                $task = DB::table('somorn_tasks')->where('somorn_tasks_id', $task_veh->somorn_tasks_id)->first();
                //查询车辆表
                $veh = DB::table('somorn_vehicle')->where('somorn_vehicle_id', $id)->first();
                $mes = "【拉法多】您的订单：{$task->tasks_no}，车牌号为{$veh->plate_number}的车辆抢单成功。";

                $title = '车辆开始执行订单';
                //车主
                $message = [
                    'targetType' => 2,
                    'target' => $veh->user_id,
                    'type' => 1,
                    'title' => $title,
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $veh->user_id, 'title' => $title, 'time' => time()]);
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                unset($web_socket);
                //搅拌站
                $message = [
                    'targetType' => 1,
                    'target' => $task->somorn_station_id,
                    'type' => 1,  //车辆订单执行
                    'title' => '车辆开始执行订单',
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                DB::table('somorn_message_log')->insert(['targetType' => 1, 'type' => 1, 'msg' => $mes, 'target' => $task->somorn_station_id, 'title' => $title, 'time' => time()]);
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                unset($web_socket);
                //通过车辆查询司机
                $driver_user = DB::table('somorn_vehicle_driver')->where('somorn_vehicle_id', $id)->get()->toArray();
                $user = array_unique(array_column($driver_user, 'user_id'));
                $title = '车主抢单成功';
                $mes = "【拉法多】订单{$task->tasks_no}抢单成功。";

                //司机
                $message = [
                    'targetType' => 2,
                    'target' => $user,
                    'type' => 1,
                    'title' => $title,
                    'message' => $mes,
                    'extend' => ['type' => 6]
                ];
                foreach ($user as $item) {
                    DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $item, 'title' => $title, 'time' => time()]);
                }
                $web_socket = new \WebSocketClient();
                $web_socket->connect('219.153.15.99', '3004', '/');
                $web_socket->sendData('notice', $message);
                break;

        }

    }

    //车主取消订单
    public function vehicleOrder($id)
    {

        $taskVehicle = TaskVehicle::where('somorn_tasks_vehicle_id', $id)->first(['somorn_tasks_id']);
        $station = Task::where('somorn_tasks_id', $taskVehicle->somorn_tasks_id)->first(['somorn_station_id', 'tasks_no']);
        $mes = "【拉法多】你发布的任务（订单号：{$station->tasks_no}），因车主原因已取消该订单。";
        $message = [
            'targetType' => 1,
            'target' => $station->somorn_station_id,
            'type' => 1,
            'title' => '订单取消',
            'message' => $mes,
            'extend' => ['type' => 6]
        ];
        DB::table('somorn_message_log')->insert(['targetType' => 1, 'type' => 1, 'msg' => $mes, 'target' => $station->somorn_station_id, 'title' => '订单取消', 'time' => time()]);
        $web_socket = new \WebSocketClient();
        $web_socket->connect('219.153.15.99', '3004', '/');
        $web_socket->sendData('notice', $message);
    }

    /**
     * socket    搅拌站发布新的订单
     * URL: POST /api/pc_platform/station/task/list/detail
     * @param id int 审核id
     * @return JSON { code: 状态码, msg: 信息描述 , result: 返回结果 }
     * 用户（搅拌站、车主、司机）发起资料修改后的审核申请，应对后台给予系统通知
     *  '接收消息对象类型', # 接收消息对象类型 0: 通知后台, 1: 通知搅拌站, 2: 通知 用户, 3:对用户发起广播, 4: 对所有的在线的发起广播
     * 1 新用户（车主、驾驶员、搅拌站）提交注册申请
     */
    public function release($id, $velid)
    {
        $staion = StationDetail::where('somorn_station_id', $id)->first();

        $user = DB::table('somorn_vehicle')->whereIn('somorn_vehicle_id', $velid)->get()->toArray();
        $user_id = array_unique(array_column($user, 'user_id'));
        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        $mes = "【拉法多】 您好，{$staion->title}搅拌站发布了新的订单，请及时查看";
        $title = '新的订单';
        if (count($user_id) > 0) {
            foreach ($user_id as $v) {
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $v, 'title' => $title, 'time' => time()]);
            }
        }
        $message = [
            'targetType' => 2,
            'target' => $user_id,
            'type' => 3,
            'title' => $title,
            'message' => $mes,
            'extend' => ['type' => 5]
        ];
        $re = $web_socket->sendData('notice', $message);
        unset($web_socket);
        $staff = DB::table('somorn_staff_status')->whereIn('boss_id', $user_id)->where('is_place')->get()->toArray();
        $driver = array_column($staff, 'user_id');

        $message = [
            'targetType' => 2,
            'target' => $driver,
            'type' => 3,
            'title' => $title,
            'message' => $mes,
            'extend' => ['type' => 5]
        ];
        if (count($driver) > 0) {
            foreach ($driver as $v) {
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $v, 'title' => $title, 'time' => time()]);
            }
        }
        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        $re = $web_socket->sendData('notice', $message);


    }

    /**
     * @param $id  车辆id
     */
    public function plan($id)
    {

        try {
            $info = Db::table("somorn_staff_status")->where('somorn_vehicle_id', $id)->first();

            $web_socket = new \WebSocketClient();
//          $web_socket->connect('socket.hanguosoft.com', '3004', '/');
            $web_socket->connect('219.153.15.99', '3004', '/');
            $mes = '【拉法多】 您好，你有一个新的排单任务';
            $message = [
                'targetType' => 2,
                'target' => $info->user_id,
                'type' => 1,
                'title' => '任务排单',
                'message' => $mes,
                'extend' => ['type' => 6]
            ];
//            DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $info->user_id, 'title' => '任务排单', 'time' => time()]);
            $re = $web_socket->sendData('notice', $message);
        } catch (\Exception $exception) {
            info($exception->getMessage());

        }
//        common::return_json(200, '成功获取站点列表');
    }


    /**
     * 司机上班打卡
     * @param $id 司机Id
     * * @param $status 1 上班 2下班
     */
    public function after_work($id, $status)
    {


        try {
            $web_socket = new \WebSocketClient();
//            $web_socket->connect('socket.hanguosoft.com', '3004', '/');
            $web_socket->connect('219.153.15.99', '3004', '/');
            //通司机主查询车主
            $data = DB::table('somorn_staff_status')->where('user_id', $id)->first();
            //司机信息
            $user = DB::table('somorn_users')->where('id', $id)->first();

            if ($status == 1) {
                $title = '司机上班';
                $mes = '【拉法多】 您好' . $user->username . '，已经上班了';
            }
            if ($status == 2) {
                $title = '司机下班';
                $mes = '【拉法多】 您好' . $user->username . '，已经下班了';
            }

            $message = [
                'targetType' => 2,
                'target' => $data->boss_id,
                'type' => 1,
                'title' => $title,
                'message' => $mes,
                'extend' => ['type' => 2]
            ];
            DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $data->boss_id, 'title' => '任务排单', 'time' => time()]);
            $re = $web_socket->sendData('notice', $message);
        } catch (\Exception $exception) {
            info($exception->getMessage());
        }

    }

    /**订单取消
     * @param $id 用户id
     * @param $task_id 任务id
     * @param $status 状态 1 搅拌站取消订单 2车主取消订单 3司机取消订单
     */
    public function cancel_order($id, $task_id, $status)
    {

        $task_id = Db::table("somorn_tasks_vehicle")
            ->where("somorn_tasks_vehicle_id", $task_id)
            ->value("somorn_tasks_id");
        //获取任务
        $task = DB::table('somorn_tasks')->where('somorn_tasks_id', $task_id)->first();
        //车主取消订单
        //获取用户
        $user = DB::table('somorn_users')->where('id', $id)->first();
        //发送给搅拌站
        $title = '车主取消订单';
        $mes = '【拉法多】 您好' . $task->tasks_no . '的任务已经被车主' . $user->username . '取消了';

        //发送给搅拌站
        $message = [
            'targetType' => 1,
            'target' => $task->somorn_station_id,
            'type' => 2,
            'title' => $title,
            'message' => $mes,
            'extend' => ['type' => 6]
        ];

        DB::table('somorn_message_log')->insert(['targetType' => 1, 'type' => 2, 'msg' => $mes, 'target' => $task->somorn_station_id, 'title' => $title, 'time' => time()]);
        $web_socket = new \WebSocketClient();
        $web_socket->connect('219.153.15.99', '3004', '/');
        $web_socket->sendData('notice', $message);
        unset($web_socket);
        //发给司机
        //获取司机
        $staff = DB::table('somorn_staff_status')->where('boss_id', $id)->get()->toArray();
        $driver = array_column($staff, 'user_id');
        $title = '车主取消订单';
        $mes = '【拉法多】 您好' . $task->tasks_no . '的任务已经被车主' . $user->username . '取消了';
        $message = [
            'targetType' => 2,
            'target' => $driver,
            'type' => 2,
            'title' => $title,
            'message' => $mes,
            'extend' => ['type' => 6]
        ];

        try {
            foreach ($driver as $v) {
                DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $v, 'title' => $title, 'time' => time()]);
            }
        } catch (\Exception $exception) {
            ECHO $exception->getMessage();
        }

        $web_socket = new \WebSocketClient();
        $web_socket->connect('219.153.15.99', '3004', '/');
        $re = $web_socket->sendData('notice', $message);

    }

    /**
     * 任务开始
     * @param $data 数组
     */
    public function start_task($data = array())
    {

        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        $web_socket->sendData('startTask', $data);
    }

    public function task_end($task_id)
    {

        $task = DB::table('somorn_tasks')->where('somorn_tasks_id', $task_id)->first();
        $mes = '【拉法多】 您好' . $task->tasks_no . '的任务已完成，有新的结算';
        $message = [
            'targetType' => 0,
            'target' => 1,
            'type' => 2,
            'title' => '任务结算',
            'message' => $mes
        ];
        DB::table('somorn_message_log')->insert(['targetType' => 0, 'type' => 2, 'msg' => $mes, 'target' => 1, 'title' => '任务结算', 'time' => time()]);
        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        $re = $web_socket->sendData('notice', $message);
    }

    public function end_task($data = array())
    {

        $web_socket = new \WebSocketClient();
//            $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        $web_socket->sendData('endTask', $data, function (&$status, $data) {

            if ($data['MsgType'] == 'handshake') return;

            $status = true;
        });
    }

//

    public function post_data(Request $request)
    {
        try {
            $data = $request->all();
            $val = [];
            foreach ($data as $v) {
                foreach ($v['trajectory'] as $key => $vv1) {
                    $location = json_decode($vv1, true);
                    $val[] = ['vehicle_id' => $v['cart_id'], 'lng' => $location['lng'], 'lat' => $location['lat'], 'time' => $key];
                }
            }
            DB::table('somorn_coordinates')->insert($val);
            $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
            $web_socket->connect('219.153.15.99', '3004', '/');

        } catch (\Exception $e) {
            file_put_contents('zp_test.log', var_export($e->getMessage(), true) . "BUG", 8);
        }
    }

    /**
     * 平台发起结算
     * vehicle  array  车辆id
     */
    public function open_settle($vehicle, $order_no)
    {

        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        //通过车辆id获取车主id
        $user = Vehicle::whereIn('somorn_vehicle_id', $vehicle)->get()->toarray();
        $user_id = array_column($user, 'user_id');
        $title = '平台发起结算';
        $mes = '【拉法多】 您好，编号为' . $order_no . '的任务，平台已发起结算请尽快确认';
        $message = [
            'targetType' => 2,
            'target' => $user_id,
            'type' => 1,
            'title' => $title,
            'message' => $mes
        ];
        foreach ($user_id as $v) {
            DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $v, 'title' => $title, 'time' => time()]);
        }
        $re = $web_socket->sendData('notice', $message);

    }

    /**
     * 车主确认结算
     * @param $owner 车主id
     * @param $order_id 订单id
     */
    public function owner_onfirm($order_id, $owner)
    {
        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        //查询任务
        $order = Task::where('somorn_tasks_id', $order_id)->first(['tasks_no']);
        //查询车主
        $user = User::find($owner);

        $order_no = $order->tasks_no;
        $title = '车主结算确认';
        $mes = '【拉法多】 您好，编号为' . $order_no . '的任务，' . $user->username . '车主已确认结算';
        $message = [
            'targetType' => 0,
            'target' => 1,
            'type' => 1,
            'title' => $title,
            'message' => $mes
        ];

        DB::table('somorn_message_log')->insert(['targetType' => 0, 'type' => 1, 'msg' => $mes, 'target' => 1, 'title' => $title, 'time' => time()]);

        $re = $web_socket->sendData('notice', $message);

    }

    /**
     * 结算意见反馈
     * @param $owner 车主id
     * @param $order_id 订单id
     */
    public function opinion($order_id, $owner)
    {
        $web_socket = new \WebSocketClient();
//        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
        $web_socket->connect('219.153.15.99', '3004', '/');
        //查询任务
        $order = Task::where('somorn_tasks_id', $order_id)->first(['tasks_no']);
        //查询车主
        $user = User::find($owner);

        $order_no = $order->tasks_no;
        $title = '结算意见反馈';
        $mes = '【拉法多】 您好，编号为' . $order_no . '的任务，' . $user->username . '车主有意见反馈';
        $message = [
            'targetType' => 0,
            'target' => 1,
            'type' => 1,
            'title' => $title,
            'message' => $mes
        ];

        DB::table('somorn_message_log')->insert(['targetType' => 0, 'type' => 1, 'msg' => $mes, 'target' => 1, 'title' => $title, 'time' => time()]);

        $re = $web_socket->sendData('notice', $message);
    }

    /**
     * 平台打款
     */
    public function paying($owner, $order_id)
    {
        $web_socket = new \WebSocketClient();
        $web_socket->connect('socket.hanguosoft.com', '3004', '/');
//        $web_socket->connect('219.153.15.99', '3004', '/');
        //查询任务
        $order = Task::where('somorn_tasks_id', $order_id)->first(['tasks_no']);
        //查询车主
//        $user = User::find($owner);

        $order_no = $order->tasks_no;
        $title = '平台打款';
        $mes = '【拉法多】 您好，编号为' . $order_no . '的任务，平台已打款到你的账户,请验收';
        $message = [
            'targetType' => 2,
            'target' => $owner,
            'type' => 2,
            'title' => $title,
            'message' => $mes
        ];

        DB::table('somorn_message_log')->insert(['targetType' => 2, 'type' => 2, 'msg' => $mes, 'target' => $owner, 'title' => $title, 'time' => time()]);

        $re = $web_socket->sendData('notice', $message);
    }


}
